﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.Framework.Server.Base;
using ZhimeiSoft.CharmManager.Server.DAL.Interface;

namespace ZhimeiSoft.CharmManager.Server.DAL
{
    public class EmployeeVacationDAL : BaseDal, IEmployeeVacationDAL
    {
        

        /// <summary>
        /// 设置插入、更新、删除SQL参数
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="employeeVacation">信息类</param>
        private void setSQLParameters(ref DbCommand cmd, DBEntityEmoloyeeVacation employeeVacation)
        {
            if(cmd == null || employeeVacation == null){
                throw new Exception("设置员工假期");
            }
            //@EMPLOYEEID, @VSTATE, @VSTARTDATETIME, @VENDDATETIME
            DB.AddInParameter(cmd, "EMPLOYEEID", DbType.Int16, employeeVacation.EMPLOYEEID);
            DB.AddInParameter(cmd, "VSTATE", DbType.String, employeeVacation.VSTATE);
            DB.AddInParameter(cmd, "VSTARTDATETIME", DbType.DateTime, employeeVacation.VSTARTDATETIME);
            DB.AddInParameter(cmd, "VENDDATETIME", DbType.DateTime, employeeVacation.VENDDATETIME);
        }

        public void InsertEmployeeVacation(DBEntityEmoloyeeVacation employeeVacation)
        {
            //先删除在修改
            DelEmployeeVacation(employeeVacation);

            DbCommand cmdSelect = DB.GetSqlStringCommand(EmployeeVacationDAL_Const.SQL_INSERT_EMPLOYEEVACATION);
            setSQLParameters(ref cmdSelect, employeeVacation);
            DB.ExecuteNonQuery(cmdSelect); ;
        }

        public void DelEmployeeVacation(DBEntityEmoloyeeVacation employeeVacation)
        {
            DbCommand cmdSelect = DB.GetSqlStringCommand(EmployeeVacationDAL_Const.SQL_DELETE_EMPLOYEEVACATION);
            setSQLParameters(ref cmdSelect, employeeVacation);
            DB.ExecuteNonQuery(cmdSelect);
        }

        public void UpdateEmployeeVacation(DBEntityEmoloyeeVacation employeeVacation_old, DBEntityEmoloyeeVacation employeeVacation_new)
        {
            DelEmployeeVacation(employeeVacation_old);
            InsertEmployeeVacation(employeeVacation_new);
        }

        public DataSet QueryEmployeeVacationDateTime(string startDate, string endDate)
        {
            DbCommand cmd = DB.GetSqlStringCommand(EmployeeVacationDAL_Const.SQL_QUERY_EMPLOYEEVACATION_DATETIME);
            DB.AddInParameter(cmd, "VSTARTDATETIME", DbType.DateTime, startDate);
            DB.AddInParameter(cmd, "VENDDATETIME", DbType.DateTime, endDate);
            return DB.ExecuteDataSet(cmd);
        }

        public DataSet QueryEmployeeVacationEmployeeID(int employeeID, string startDate, string endDate)
        {
            DbCommand cmd = DB.GetSqlStringCommand(EmployeeVacationDAL_Const.SQL_QUERY_EMPLOYEEVACATION_DATETIME_EMPLOYEEID);
            DB.AddInParameter(cmd, "EMPLOYEEID", DbType.Int32, employeeID);
            DB.AddInParameter(cmd, "VSTARTDATETIME", DbType.DateTime, startDate);
            DB.AddInParameter(cmd, "VENDDATETIME", DbType.DateTime, endDate);
            return DB.ExecuteDataSet(cmd);
        }

        public DataSet QueryEmployeeVacationDay(string vacationDate, string state)
        {
            DbCommand cmd;
            if (state.Equals(string.Empty))
            {
                cmd = DB.GetSqlStringCommand(EmployeeVacationDAL_Const.SQL_QUERY_EMPLOYEEVACATION_DATETIME_DAY);
            }
            else
            {
                cmd = DB.GetSqlStringCommand(EmployeeVacationDAL_Const.SQL_QUERY_EMPLOYEEVACATION_DATETIME_DAYA_STATE);
                DB.AddInParameter(cmd, "VSTATE", DbType.String, state);
            }
            DB.AddInParameter(cmd, "QUERYTIME", DbType.DateTime, vacationDate);
            return DB.ExecuteDataSet(cmd);

        }
    }
}
